home *** CD-ROM | disk | FTP | other *** search
/ CU Amiga Super CD-ROM 19 / CU Amiga Magazine's Super CD-ROM 19 (1998)(EMAP Images)(GB)[!][issue 1998-02].iso / CUCD / Online / NNTPd / server / xindex.c < prev    next >
Encoding:
C/C++ Source or Header  |  2002-11-01  |  3.7 KB  |  167 lines

  1. /*
  2.  *  Project   : NNTP (RFC 977) extension
  3.  *  Module    : xindex.c
  4.  *  Author    : I.Lea
  5.  *  Created   : 07-03-92
  6.  *  Updated   : 18-11-92
  7.  *  Notes     : Add a command to retieve tin style index files 
  8.  *              from the NNTP server so as to save space on the
  9.  *              client.
  10.  *              Ideas borrowed from XTHREAD nntp extension code
  11.  *              posted by Tim Iverson to alt.sources in mid'91.
  12.  *  Copyright : (c) Copyright 1992 by Iain Lea
  13.  *              You may  freely  copy or  redistribute  this software,
  14.  *              so  long as there is no profit made from its use, sale
  15.  *              trade or  reproduction.  You may not change this copy-
  16.  *              right notice, and it must be included in any copy made
  17.  */
  18.  
  19. #include "common.h"
  20. #ifndef lint
  21. static char sccsid[] = "$Id: xindex.c,v 1.3 1994/11/01 07:24:58 sob Exp sob $";
  22. #endif
  23. #ifdef XINDEX
  24.  
  25. #undef    DEBUG_XINDEX        /* set to define to turn on more debug info */
  26. #define HASH_VALUE 1409        /* mod value for hashing group name */
  27.  
  28. #ifndef MAXPATHLEN
  29. #    define MAXPATHLEN 256
  30. #endif
  31.  
  32. #ifdef __STDC__
  33. void xindex (int argc, char *argv[]);
  34. static void find_index_file (char *group, char *index_file);
  35. static long hash_groupname (char *group);
  36. #else
  37. void xindex ();
  38. static void find_index_file ();
  39. static long hash_groupname ();
  40. #endif
  41.  
  42. /*
  43.  *  Usage: XINDEX groupname
  44.  *
  45.  *  Retrieve an index file for the specified newsgroup (ie. alt.sources)
  46.  *
  47.  *  This command is NOT documented in RFC977.
  48.  */
  49.  
  50. void xindex (argc, argv)
  51.     int    argc;
  52.     char    *argv[];
  53. {
  54.     char    line[NNTP_STRLEN];
  55.     char    group[MAXPATHLEN];
  56.     char    index_file[MAXPATHLEN];
  57.     char    *cp;
  58.     FILE    *fp;
  59.     
  60.     /*
  61.      * "parse" the argument list
  62.      */
  63.     if (argc == 1) {
  64.         printf("%d Usage: XINDEX group\r\n", ERR_CMDSYN);
  65.         (void) fflush(stdout);
  66.         return;
  67.     } else {
  68.         strncpy (group, argv[1], sizeof (group)-1);
  69. #if defined(SYSLOG) && defined(DEBUG_XINDEX)
  70.         syslog(LOG_INFO, "%s xindex %s", hostname, group);
  71. #endif
  72.  
  73.         find_index_file(group, index_file);
  74.         
  75.         if ((fp = fopen(index_file, "r")) == NULL) {
  76. #ifdef SYSLOG
  77.             syslog(LOG_INFO, "%s xindex cannot open %s (%s)",
  78.                 hostname, group, index_file);
  79. #endif
  80.             printf("%d XINDEX Cannot open %s\r\n",
  81.                 ERR_XINDEX, group);
  82.             (void) fflush(stdout);
  83.             return;
  84.         }
  85.  
  86.         printf("%d XINDEX %s index file follows\r\n", 
  87.             OK_XINDEX, group);
  88.         (void) fflush(stdout);
  89.         
  90.         while (fgets(line, sizeof(line), fp) != NULL) {
  91.             if ((cp = index(line, '\n')) != NULL)
  92.                 *cp = '\0';
  93.             putline(line);
  94.         }
  95.         (void) fclose(fp);
  96.     
  97.         putline(".");
  98.         (void) fflush(stdout);
  99.     }
  100. }
  101.  
  102. /*
  103.  *  Look in XINDEX_DIR dir (defined in conf.h) for the index file 
  104.  *  for the given group. Hashing the group name gets a number. 
  105.  *  See if that #.1 file exists; if so, read first line. Is it the
  106.  *  Group we want? If not try #.2. Repeat until no such file or we 
  107.  *  find the right file.
  108.  */
  109.  
  110. static void find_index_file (group, index_file)
  111.     char *group;
  112.     char *index_file;
  113. {
  114.     char buf[MAXPATHLEN], *p;
  115.     FILE *fp;
  116.     int i = 1;
  117.     unsigned long hash;
  118.     struct stat sb;
  119.     
  120.     hash = hash_groupname (group);
  121.  
  122.     while (1) {
  123.         sprintf (index_file, "%s/%lu.%d", XINDEX_DIR, hash, i);
  124.         
  125.         if ((fp = fopen (index_file, "r")) == NULL) {
  126.             return;
  127.         }
  128.  
  129.         if (fgets (buf, sizeof (buf), fp) == NULL) {
  130.             fclose (fp);
  131.             return;
  132.         }
  133.         fclose (fp);
  134.  
  135.         for (p = buf; *p && *p != '\n'; p++) {
  136.             continue;
  137.         }    
  138.         *p = '\0';
  139.  
  140.         if (strcmp (buf, group) == 0) {
  141.             return;
  142.         }    
  143.         i++;
  144.     }    
  145. }
  146.  
  147. /*
  148.  * hash group name for filename of group
  149.  */
  150.  
  151. static long hash_groupname (group)
  152.     char *group;
  153. {
  154.     unsigned long hash_value;
  155.     unsigned char *ptr = (unsigned char *) group;
  156.  
  157.     hash_value = *ptr++;
  158.  
  159.     while (*ptr)
  160.         hash_value = ((hash_value << 1) ^ *ptr++) % HASH_VALUE;
  161.  
  162.     return (hash_value);
  163. }
  164.  
  165. #endif /* XINDEX */
  166.  
  167.